// pages/juhui/juhui.js
var app=getApp();
var ddapi = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,
    interval: 3000,
    duration: 1000,
    goods:[],
    banners:[],
    swiperCurrent: 0,
    page:1,
    page_count:10,
    keyword:'',
    loadmore:true,
    t_type: 0,
    openid: '',
    user_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    var t_type = e.type
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'Data.Banner';
    var sign = ddapi.md5(openid+service+4+user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service='+service,
      data: {
        user_id:user_id,
        open_id:openid,
        sign:sign,
        type:4
      },
      header: {
        'Content-Type': 'json'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          banners:res.data.data.info
        });
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getProductList: function () {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    var that = this 
    var page = that.data.page
    var page_count = that.data.page_count
    console.log(page_count)
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'Product.ProductList';
    var sign = ddapi.md5(openid + page + page_count + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      data: {
        user_id: user_id,
        openid: openid,
        sign: sign,
        page:page,
        page_count:page_count
      },
      header: {
        'Content-Type': 'json'
      },
      success: function (res) {
        wx.hideToast()
        console.log(res)
        if (res.data.data.info.length>0){
          var goods = that.data.goods.concat(res.data.data.info)
          that.setData({
            goods: goods,
            loadmore: true,
          });
        }else{
          that.setData({
            loadmore: false,
          });
        }
      },
      fail: function (res) {
        wx.hideToast()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var user_phone = wx.getStorageSync('userPhone');
    var len = that.data.goods.length
    if (user_phone) {
      if (len==0){
        that.getProductList()
      }else{

      }
    } else {
      var url = '/pages/login/login';
      wx.navigateTo({
        url: url
      })
    }
    that.setData({
      openid: wx.getStorageSync('openid'),
      user_id: wx.getStorageSync('user_id'),
    })
    that.getlod();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.keyword==''){
      if (this.data.loadmore) {
        var page = Number(this.data.page) + Number(1);
        console.log(page)
        this.setData({
          page: page
        });
        this.getProductList()
      } else {
        wx.showToast({
          title: '暂无更多',
          image: '../../images/popup-close.png'
        })
      }
    }
  },
  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  wxSearchInput: function (e){
    this.setData({
      keyword: e.detail.value
    })
  },
  wxSearchFn: function () {
    wx.showToast({
      title: '搜索中',
      icon: 'loading'
    })
    var that = this 
    var keyword = this.data.keyword;
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'Product.SearchProduct';
    var sign = ddapi.md5(keyword + openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Product.SearchProduct',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        openid: openid,
        user_id:user_id,
        keyword:keyword
      },
      success: function (res) {
        wx.hideToast()
        console.log(res)
        var goods = res.data.data.info
        that.setData({
          goods: goods,
          keyword: ''
        })
      },
      fail : function (res) {
        wx.hideToast()
      }
    })
  },
  toDetailsTap:function (){
    console.log(213);
    // wx.navigateTo({
    //   url: "/pages/product-details/index?id=" + e.currentTarget.dataset.id
    // })
    wx.navigateToMiniProgram({
      appId: 'wx24897ab4efea5b3d',
      path: 'pages/start/start',
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },
  showMap:function (){
    var url = '/pages/map/map';
    wx.switchTab({
      url: url,
    });
  },
  tiaozhuan:function(e){
    var type = e.currentTarget.dataset.id
    if(type==1){
      var url = '/pages/juhui/juhui';
      wx.switchTab({
        url: url,
      });
    } else{
      var url = '/pages/ucenter/fxyj/fxyj';
      wx.navigateTo({
        url: url,
      });
    }
  },
  getlod: function () {
    var that = this;
    var service = 'User.CheckLand';
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.CheckLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.user_phone = '';
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        }
      }
    })
  },
})